package com.example.hotel_lxy.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.adapter.RVAdapter;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanDate;
import com.example.hotel_lxy.bean.EventBusBean;
import com.example.hotel_lxy.utils.DateUtils;
import com.ysxsoft.common_base.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Administrator
 */
@Route(path = "/main/CalendarActivity2")
public class CalendarActivity2 extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    private RecyclerView recyclerView;
    private List<BeanDate> mlsit = new ArrayList<>();
    private RVAdapter rvAdapter;
    @Autowired
    int y;
    @Autowired
    int m;
    @Autowired
    int type; // 1 不可选择之前的  2 都可以
    private TextView normalHead;
    private int headHeight;
    private int mCurrentPosition = 0;
    private LinearLayout line1;

    public static void start(int y, int m,int type ) {
        ARouter.getInstance().build(ARouterPath.getCalendarActivity2())
                .withInt("y", y)
                .withInt("m", m)
                .withInt("type", type)
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_calendar2;
    }

    @Override
    public void doWork() {
        super.doWork();
        ARouter.getInstance().inject(this);
        normalHead = findViewById(R.id.tv_head);
        recyclerView = findViewById(R.id.recyclerview);
        line1 = findViewById(R.id.line1);
        normalHead.setText(y + "年" + (m + 1) + "月");
        for (int i = 0; i < 12; i++) {
            if (m >= 12) {
                y = y + 1;
                m = 1;
            } else {
                m = m + 1;
            }
            BeanDate beanDate = new BeanDate();
            List<String> todayString = DateUtils.getTodayString(y, m);
            beanDate.setTitle(y + "年" + m + "月");
            beanDate.setMlist(todayString);
            mlsit.add(beanDate);
        }
        initinfo();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    LinearLayoutManager linearLayoutManager;

    private void initinfo() {
        linearLayoutManager = new LinearLayoutManager(this);
        rvAdapter = new RVAdapter(CalendarActivity2.this, mlsit,type);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(rvAdapter);
        recyclerView.addOnScrollListener(loadingMoreListener);
        rvAdapter.setOnDeleteListener(new RVAdapter.OnDeleteListener() {
            @Override
            public void delete(String position) {
                EventBusBean eventBusBean = new EventBusBean();
                eventBusBean.setStarttime(position);
                eventBusBean.setType(1);
                EventBus.getDefault().post(eventBusBean); //普通事件发布
                finish();

            }
        });
    }

    RecyclerView.OnScrollListener loadingMoreListener = new RecyclerView.OnScrollListener() {
        //到达顶部和底部监听
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            int i = normalHead.getHeight();
            headHeight = i;
//            第一项和最后一项
            // OnScrollListener.SCROLL_STATE_IDLE; //停止滑动状态
            // 记录当前滑动状态
            if (newState == RecyclerView.SCROLL_STATE_DRAGGING) { //当前状态为停止滑动
                if (!recyclerView.canScrollVertically(1)) { // 到达底部
                    initNext();
                } else if (!recyclerView.canScrollVertically(-1)) { // 到达顶部
                    initAdd();
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            View itemView = linearLayoutManager.findViewByPosition(mCurrentPosition + 1);
            if (itemView != null) {
                // 100      110  10
                if (itemView.getTop() <= headHeight) {
                    normalHead.setY(-(headHeight - itemView.getTop()));
                } else {
                    normalHead.setY(0);
                }
            }
            //拿到当前显示的item的position
            int currentPosition = linearLayoutManager.findFirstVisibleItemPosition();
            if (mCurrentPosition != currentPosition) {
                mCurrentPosition = currentPosition;
                normalHead.setY(0);
                // 拿到当前item的head值,显示在normalHead上
                normalHead.setText(mlsit.get(mCurrentPosition).getTitle());
            }
        }
    };

    private void initNext() {
        String title = mlsit.get(mlsit.size() - 1).getTitle();
        String[] split = title.replace("年", "-").replace("月", "-").split("-");
        y = Integer.valueOf(split[0]);
        m = Integer.valueOf(split[1]);
        for (int i = 0; i < 12; i++) {
            if (m >= 12) {
                y = y + 1;
                m = 1;
            } else {
                m = m + 1;
            }
            BeanDate beanDate = new BeanDate();
            List<String> todayString = DateUtils.getTodayString(y, m);
            beanDate.setTitle(y + "年" + m + "月");
            beanDate.setMlist(todayString);
            mlsit.add(beanDate);
        }
        rvAdapter.notifyDataSetChanged();

    }

    private void initAdd() {
        String title = mlsit.get(0).getTitle();
        String[] split = title.replace("年", "-").replace("月", "-").split("-");
        y = Integer.valueOf(split[0]);
        m = Integer.valueOf(split[1]);
        for (int i = 0; i < 12; i++) {
            if (m <= 1) {
                y = y - 1;
                m = 12;
            } else {
                m = m - 1;
            }
            BeanDate beanDate = new BeanDate();
            List<String> todayString = DateUtils.getTodayString(y, m);
            beanDate.setTitle(y + "年" + m + "月");
            beanDate.setMlist(todayString);
            mlsit.add(0, beanDate);
        }
        recyclerView.scrollToPosition(13);
        rvAdapter.notifyDataSetChanged();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}